<?php
namespace app\index\Controller;

use app\index\model\Companymodel;
use think\captcha\Captcha;
use think\Controller;
use think\Loader;
use think\Request;
use think\Session;
use think\Db;

class Company extends Controller{
//企业邮箱登录
    public function index(){
        $request = Request::instance();
        $caback=$request->get('callback');
        $emaill=$request->get('email');
        $comppwd=md5($request->get('comppwd'));
        $smscode = $request->request('smsCode');

//        echo $caback."(.$comppwd.)";
//        exit;

        $model = new Companymodel();
        $res = $model->emile($emaill);


        $ran = Session::get("enter_code");

        if($res['comppwd'] !=$comppwd || $comppwd ==""){
            echo $caback."(1)";
        }else if($smscode != $ran){
            echo $caback."(2)";
        }else{

            Session::set("companyId",$res['id']);
            Session::set("enter_code",null);
            echo $caback."(3)";

        }
    }


//企业用户名登录
    public function login(){
        $request = Request::instance();
        $caback=$request->get('callback');
        $compname=$request->get('compname');
        $comppwd = md5($request->get('comppwd'));
        $smscode = $request->request('smscode');

        $model = new Companymodel();
        $code = new Captcha();
        $res = $model->company($compname);
        $very = $code->check($smscode);

//        dump($very);

        if($res['compname']!=$compname ||$compname==""){
            echo $caback."(0)";
            return;
        }else if ($res['comppwd'] !=$comppwd){
            echo $caback."(1)";
            return;
        }else if($very!=true){
            echo $caback."(2)";
        }else{
            Session::set("companyId",$res["id"]);
            echo $caback."(3)";
        }

    }



    //邮箱验证
    public function code(){
       $request= Request::instance();
        $callback = $request->get('callback');
        $emaill = $request->get('emaill');
        $rand =  trim(rand(1000,9999));
        session('enter_code',$rand);

        $model = new Companymodel();
        $em  = $model->emile($emaill);
        if($em){
            echo $callback ."(3)";
            return;
        }else{
            echo $callback ."(2)";
            Loader::import('Maile.PHPMailerAutoload');
            $mail = new \PHPMailer(true); //建立邮件发送类
            $mail->CharSet = "UTF-8";//设置信息的编码类型
            $address = "$emaill";//收件人地址
            $mail->IsSMTP(); // 使用SMTP方式发送
            $mail->Host = "smtp.163.com"; //使用163邮箱服务器
            $mail->SMTPAuth = true; // 启用SMTP验证功能
            $mail->Username = "z869886933@163.com"; //你的163服务器邮箱账号
            $mail->Password = "Angel8698"; // 163邮箱密码
            $mail->Port = 25;//邮箱服务器端口号
            $mail->From = "z869886933@163.com"; //邮件发送者email地址
            $mail->FromName = "注册验证码";//发件人名称
            $mail->AddAddress("$address", $name=''); //收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
            //   $mail->AddAttachment("D:abc.txt"); // 添加附件(注意：路径不能有中文)
            $mail->IsHTML(true);//是否使用HTML格式
            $mail->Subject = "公司注册验证码"; //邮件标题
            $mail->Body = "$rand"; //邮件内容，上面设置HTML，则可以是HTML
            if($address==""){
                echo $callback."(2)";
                return;
            }

            if (!$mail->Send()) {
                echo true;
                exit;
            }else {
                echo false;
            }
//            if($mail->Send()){
//                echo $callback."(1)";
//                return;
//            } else{
//                echo  $callback."(0)";
//            }
            return;
        }

     }

//邮箱注册
    public function register(){
        $request = Request::instance();
        $callback = $request->get('callback');
        $compname = $request->get('compname');
        $emaill = $request->get('emaill');
        $comppwd =$request->get('comppwd');
        $code1 = $request->get('smsCode');

//        echo $callback."(.$comppwd.)";
//        $code = session('code');
   //验证公司名是否存在
        $pname = new Companymodel();
        $plan = $pname->company($compname);
        if($plan){
            echo $callback ."(0)";
            return;
        }
        $model = new Companymodel();
        $em  = $model->emile($emaill);
        if($em){
            echo $callback ."(5)";
            return;
        }
        //验证码判断 成功则写入数据
        $ran = Session::get("enter_code");
        if($code1 == $ran){
            $user = new Companymodel();
            $data = array(
            'compname' => $compname,
            'emaill' => $emaill,
            'comppwd' =>md5($comppwd),
            'regtime' => time()
        );
            $res = $user->registe($data);
            if($res){
                Session::set("companyId",$res);
                echo $callback."(3)";
            }else{
                echo $callback."(4)";
            }
        }else{
            echo $callback."(2)";
             return;
        }

        }
    public function smscode(){
        $request= Request::instance();
        $callback = $request->get('callback');
        $emaill = $request->get('emaill');
        $rand =  trim(rand(1000,9999));
        session('enter_code',$rand);

        $model = new Companymodel();
        $em  = $model->emile($emaill);
        if($em){
            echo $callback ."(2)";
            Loader::import('Maile.PHPMailerAutoload');
            $mail = new \PHPMailer(true); //建立邮件发送类
            $mail->CharSet = "UTF-8";//设置信息的编码类型
            $address = "$emaill";//收件人地址
            $mail->IsSMTP(); // 使用SMTP方式发送
            $mail->Host = "smtp.163.com"; //使用163邮箱服务器
            $mail->SMTPAuth = true; // 启用SMTP验证功能
            $mail->Username = "z869886933@163.com"; //你的163服务器邮箱账号
            $mail->Password = "Angel8698"; // 163邮箱密码
            $mail->Port = 25;//邮箱服务器端口号
            $mail->From = "z869886933@163.com"; //邮件发送者email地址
            $mail->FromName = "注册验证码";//发件人名称
            $mail->AddAddress("$address", $name=''); //收件人地址，可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
            //   $mail->AddAttachment("D:abc.txt"); // 添加附件(注意：路径不能有中文)
            $mail->IsHTML(true);//是否使用HTML格式
            $mail->Subject = "公司注册验证码"; //邮件标题
            $mail->Body = "$rand"; //邮件内容，上面设置HTML，则可以是HTML
            if($address==""){
                echo $callback."(2)";
                return;
            }

            if (!$mail->Send()) {
                echo true;
                exit;
            }else {
                echo false;
            }
//            if($mail->Send()){
//                echo $callback."(1)";
//                return;
//            } else{
//                echo  $callback."(0)";
//            }
            return;

        }else{
            echo $callback ."(3)";
            return;
        }

    }
    public function info(){
        $request= Request::instance();
        $callback = $request->get('callback');
        $companyId=Session::get("companyId");
        $model=new Companymodel();
        $re=$model->info1($companyId);
        echo   $callback."(".json_encode($re).")";
//        echo Db::table("rs_company")->where('id',$companyId)->fetchSql()->select();

    }
    public function set()
    {
        $request = Request::instance();
        $callback = $request->get('callback');
        $companyId = Session::get("companyId");
        $placename = $request->get('placename');
        $salary = $request->get('salary');
        $education = $request->get('education');
        $wrokeInfo = $request->get('wrokeInfo');
        if ($companyId == null || $placename == null || $salary == null || $education == null || $wrokeInfo == null) {
            echo $callback . "(0)";
        } else {
            $model = new Companymodel();
            $data = array('companyId' => $companyId, 'placename' => $placename, 'salary' => $salary, 'education' => $education, 'wrokeInfo' => $wrokeInfo,
                'createTime' => time());
            $re = $model->set1($data);

            if ($re == true) {
                echo $callback . "(1)";
            } else {
                echo $callback . "(2)";
            }

        }
    }
// 查询用邮箱登录的验证码名字；
 function getCompayname(){
     $request = Request::instance();
     $callback = $request->get('callback');
     $conpnyId['id']=  Session::get("companyId");
    $companyName=    Db::name("company")->where($conpnyId)->find();
     if($companyName['compname']){
          echo $callback."(".json_encode($companyName['compname']).")";
     }else{
         echo $callback."(0)";
     }
 }
//    上传保存头像;
 function  uploadCompanyurl(){
     if ((($_FILES["file"]["type"] == "image/gif")
             || ($_FILES["file"]["type"] == "image/jpeg")
             || ($_FILES["file"]["type"] == "image/pjpeg"))
         && ($_FILES["file"]["size"] < 2000000))
     {
         if ($_FILES["file"]["error"] > 0)
         {
             $data = array(
                 'error' =>  $_FILES["file"]["error"],
                 'msg'   => 'some error happend',
                 'imgurl' => null,
             );
             $url = 'http://www.home.com/uploadImg.php';
             $data = json_encode($data);
             header('Location:' .$url . '?data=' . $data);
         }
         else
         {
             if (file_exists("./static/upload/" . $_FILES["file"]["name"]))
             {
                 $data = array(
                     'error' =>  null,
                     'msg'   => 'file already exists',
                     'imgurl' => null,
                 );
                 $url = 'http://www.home.com/uploadImg.php';
                 $data = json_encode($data);
                 header('Location:' .$url . '?data=' . $data);
             }
             else
             {
                 $imgName= explode(".",$_FILES["file"]["name"]);
//                    ????????????????UPload????????��??????��?????
                 $imginfo=time().".".$imgName[1];
                 $img['headUrl']=$imginfo;
                 move_uploaded_file($_FILES["file"]["tmp_name"], "./static/upload/" .$imginfo);

//                  写入数据库；
                 $url["companyUrl"]= 'http://www.admin.com/static/upload/'. $imginfo;
                   $companyId['id']=   Session::get("companyId");
                 Db::name("company")->where($companyId)->update($url);


                 $data = array(
                     'error' =>  null,
                     'msg'   => 'upload success',
                     'imgurl' => 'http://www.admin.com/static/upload/'. $imginfo,
                 );
                 $url = 'http://www.home.com/uploadImg.php';
                 $data = json_encode($data);
                 header('Location:' .$url . '?data=' . $data);
                 exit;
             }

         }
     }
     else
     {
         $data = array(
             'error' =>  null,
             'msg'   => 'file type not allowed',
             'imgurl' =>null,
         );
         $url = 'http://www.home.com/uploadImg.php';
         $data = json_encode($data);
         header('Location:' .$url . '?data=' . $data);
     }
 }
//获取登录公司的头像 ；
 public  function getCompanyURL(){
     $callback= Request::instance()->param("callback");
     $data['id']=  Session::get("companyId");
     $reslut=   Db::name("company")->where($data)->field("companyUrl")->find();
    if($reslut){
        echo $callback."(".json_encode($reslut).")";
    }else{
         echo $callback."(0)";
    }
 }


}